<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Report;
use App\Model\MessageText;
use App\Model\Message;
use Validator;
use App\User;
use Exception;
use Auth;
use Hash;

class UserController extends Controller
{

    public function resetPassword(Request $request)
    {
        $this->validateResetPassword($request);

        $newpassword = $request->input("newpassword");
        if(!preg_match('/^(?=.*[a-z]).*$/', $newpassword)){
            return response()->json(array('message'=>'密码至少包含一个小写字母', 'code'=>400), 200);
        }
        if(!preg_match('/^(?=.*[A-Z]).*$/', $newpassword)){
            return response()->json(array('message'=>'密码至少包含一个大写字母', 'code'=>400), 200);
        }
        if(!preg_match('/^(?=.*[0-9]).*$/', $newpassword)){
            return response()->json(array('message'=>'密码至少包含一个数字', 'code'=>400), 200);
        }
        if(!preg_match('/^(?=.*[_!@#$%^&*]).*$/', $newpassword)){
            return response()->json(array('message'=>'密码至少包含一个特殊字符', 'code'=>400), 200);
        }
        if(!preg_match('/^[A-Za-z0-9_!@#$%^&*]{10,}$/', $newpassword)){
            return response()->json(array('message'=>'密码不能少于10位', 'code'=>400), 200);
        }



        $password = $request->input("password");
        $user = $this->guard()->user();
        if($user) {
            /*验证密码是否正确*/
            $data_user = User::where('id', $user['id'])->select(array('id','name','password'))->first();
            if(!Hash::check($password, $data_user['password'])) {
                return response()->json(array('message'=>'密码不正确', 'code'=>400), 200);
            }
            $user->password = bcrypt($newpassword);
            $user->remember_token = '';
            $user->save();
            return response()->json(array('message'=>'修改密码成功', 'code'=>200), 200);
        }

        return response()->json(array('message'=>'修改密码失败', 'code'=>400), 200);

    }

    protected function validateResetPassword(Request $request)
    {
        $this->validate($request, 
            ['password'=>'bail|required', 'newpassword'=>'bail|required|min:10', 'repassword'=>'bail|required|same:newpassword'], 
            ['password.required'=>'请输入密码', 'newpassword.required'=>'请输入新密码', 'newpassword.min'=>'密码长度最少是10位','repassword.required'=>'请输入确认密码', 'repassword.same'=>'新密码与确认密码不匹配']
        );
    }

    protected function guard()
    {
        return Auth()->guard('api');
    }

    protected function formatValidationErrors2(Validator $validator)
    {
        $errors = $validator->errors()->all();
        throw new Exception($errors[0], 1);  
    }
    public function getreportlist(Request $request){
        $res=Report::where('user_id',Auth::user()->id)->orderBy('created_at','desc')->paginate($request->get('limit',10))->toArray();
        $data = [
            'code' => 1,
            'msg'   => '请求成功',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }
    public function addreport(Request $req){
        $rules = [
            'title' => 'required'
        ];
        $messages = [
            'title.required' => '日报标题未填写',
        ];
        $validator = Validator::make($req->all(),$rules,$messages);
        if ($validator->fails()) {
            return response()->json(['code'=>0,'msg' => $validator->errors()->first()], 200);
        }
        $userid=Auth::user()->id;
        $report=new Report();
        $report->title=$req->input('title');
        $report->content=$req->input('content');
        $report->user_id=$userid;
        $report->status=1;
        $report->day=date('Y-m-d');
        $report->save();

        $data=[
            'code'=>1,
            'data'=>'填报成功'
        ];
        return response()->json($data);
    }
    public function getmsglist(Request $request){
        $res=Message::with(['frominfo'=>function($query){
            $query->select('id','name');
        },'messageText'=>function($query){
            $query->select('id','title');
        }])->where('to_id',Auth::user()->id)->orderBy('id','desc')->paginate($request->get('limit',10))->toArray();
        $data = [
            'code' => 1,
            'msg'   => '请求成功',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }
    // 读消息
    public function getmsgdetail(Request $req){
        $id=$req->input('id');
        $msg=Message::with('messageText')->where('id',$id)->first();
        $msg->status=1;
        $msg->save();
        $data = [
            'code' => 1,
            'msg'   => '请求成功',
            'data'  => $msg
        ];
        return response()->json($data);
    }
}
